package com.imooc.springboot.dubbo.nilinili.provider.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.imooc.springboot.dubbo.nilinili.entity.bean.PageBean;
import com.imooc.springboot.dubbo.nilinili.entity.dto.CollectionDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.MyCollectionDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.MyCommentDTO;
import com.imooc.springboot.dubbo.nilinili.provider.entity.po.CollectionPO;
import com.imooc.springboot.dubbo.nilinili.provider.entity.po.MyCollectionPO;
import com.imooc.springboot.dubbo.nilinili.provider.mapper.CollectionMapper;
import com.imooc.springboot.dubbo.nilinili.provider.mapper.UserMapper;
import com.imooc.springboot.dubbo.nilinili.provider.utils.BeanCopierUtil;
import com.imooc.springboot.dubbo.nilinili.provider.utils.JwtToUserId;
import com.imooc.springboot.dubbo.nilinili.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CollectionService.class)
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public boolean toggleCollection(CollectionDTO collectionDTO, String token) {
        String user_id = JwtToUserId.Jwt2UserId(token);
        CollectionPO collectionPO = BeanCopierUtil.copyProperties(collectionDTO, CollectionPO.class);
        collectionPO.setUserId(Integer.valueOf(user_id));
        Integer collected = collectionMapper.isCollected(collectionPO);
        if (collected !=null){
            Integer integer = collectionMapper.cancelCollected(collectionPO);
            return integer==0;
        }
        Integer integer = collectionMapper.addCollection(collectionPO);
        return integer > 0;
    }

    @Override
    public boolean isCollected(CollectionDTO collectionDTO, String token) {
        String user_id = JwtToUserId.Jwt2UserId(token);
        CollectionPO collectionPO = BeanCopierUtil.copyProperties(collectionDTO, CollectionPO.class);
        collectionPO.setUserId(Integer.valueOf(user_id));
        Integer integer = collectionMapper.isCollected(collectionPO);
        return integer!=null;
    }

    @Override
    public PageBean<MyCollectionDTO> listCollection(String token,Integer pageNum) {
        String userId = JwtToUserId.Jwt2UserId(token);
        PageHelper.startPage(pageNum, 10);
        List<MyCollectionPO> collectionList = collectionMapper.getCollectionList(Integer.valueOf(userId));
        PageBean<MyCollectionPO> pageInfo = new PageBean<>(collectionList);
        PageBean<MyCollectionDTO> pageBean = BeanCopierUtil.CopyPageBean(pageInfo,MyCollectionDTO.class);
        return pageBean;
    }

    @Override
    @Transactional
    public Integer countCollection(String userId) {
        Integer integer = collectionMapper.countCollection(Integer.valueOf(userId));
        userMapper.setNewCollectionCount(userId,String.valueOf(integer));
        return integer;
    }
}
